/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.DbExecutorIface;
import ge.info.domain.StringString;
import ge.info.domain.transport.DayOfWeek;
import ge.info.logging.Session;
import ge.info.sip.SessionMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.tapestry.IRequestCycle;


import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.html.BasePage;
//import org.apache.tapestry.event.PageEvent;

public abstract class Home extends BasePage
{

    @InjectObject("spring:CurrentSessions")
    public abstract SessionMap getCurrentSessions();

    @InjectObject("spring:PreviousSessions")
    public abstract SessionMap getPreviousSessions();

    @InjectObject("spring:dbExecutor")
    public abstract DbExecutorIface getDbExecutor();

    @InjectObject("service:tapestry.globals.HttpServletRequest")
    public abstract HttpServletRequest getRequest();

    @InjectObject("spring:week")
    public abstract DayOfWeek getDayOfWeek();

    public abstract String getErrorString();

    public abstract void setErrorString(String errString);
//    @Persist

    public abstract String getUserName();

    public abstract void setUserName(String userName);
//    @Parameter

    public abstract String getPassword();

    public abstract void setPassword(String password);
//    @Parameter

    public abstract String getNewPassword();

    public abstract void setNewPassword(String newPassword);
//    @Parameter

    public abstract String getNewPasswordCheck();

    public abstract void setNewPasswordCheck(String newPasswordCheck);

    @InjectObject("engine-service:page")
    public abstract IEngineService getPageService();

    public void formSubmit(IRequestCycle cycle)
    {
        try {
            getDayOfWeek();
        if (getUserName() == null || getUserName().length() == 0 || getPassword() == null || getPassword().length() == 0)
        {
            return;
        }
        StringString user = new StringString();
        user.setStringOne(getUserName());
        user.setStringTwo(getPassword());
        //= getDbExecutor().loginCheck();
        if (!getDbExecutor().loginCheck(user))
        {
            return;
        }
        Session session = new Session();
        session.setMachineIP(getRequest().getRemoteAddr());
        session.setUserName(getUserName());
        session.setCallType(-1);
        session.setParentID("0");

        Session oldEvent = getCurrentSessions().AddOrReplace(session);
        if (oldEvent != null)
        {
            getPreviousSessions().AddOrReplace(oldEvent);
        }
        getDbExecutor().insertSession(session);
        cycle.activate(cycle.getPage("Main"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
